package com.im323.quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzSimpleManager {

	private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
	private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
	private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";

	/**
	 * 
	 * @description 添加一个定时任务，使用默认的任务组名，触发器名，触发器组名
	 * @author 王璞
	 * @date 2017年10月24日下午6:31:04
	 * 
	 * @param jobName
	 *            任务名
	 * @param cls
	 *            任务
	 * @param time
	 *            cron表达式
	 */
	public static void addJob(String jobName, Class<? extends Job> cls, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName + "_JOB", JOB_GROUP_NAME).build();
			// 触发器
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName + "_TRIGGER", TRIGGER_GROUP_NAME)
					.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
			sched.scheduleJob(jobDetail, trigger);
			// 启动
			if (!sched.isShutdown()) {
				sched.start();
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @description: 修改一个任务的触发时间(使用默认的任务组名，触发器名，触发器组名)
	 * @author 王璞
	 * @date 2017年10月24日下午6:27:24
	 */
	public static void modifyJobTime(String jobName, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			TriggerKey triggerKey = new TriggerKey(jobName + "_TRIGGER", TRIGGER_GROUP_NAME);

			CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
			if (trigger == null) {
				return;
			}
			String oldTime = trigger.getCronExpression();
			if (!oldTime.equalsIgnoreCase(time)) {
				JobKey jobKey = new JobKey(jobName + "_JOB", JOB_GROUP_NAME);
				JobDetail jobDetail = sched.getJobDetail(jobKey);
				if (jobDetail == null) {
					return;
				}
				Class<? extends Job> objJobClass = jobDetail.getJobClass();
				removeJob(jobName);
				addJob(jobName, objJobClass, time);
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @description 移除一个任务(使用默认的任务组名，触发器名，触发器组名)
	 * @author 王璞
	 * @date 2017年10月24日下午6:27:24
	 */
	public static void removeJob(String jobName) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			TriggerKey triggerKey = new TriggerKey(jobName + "_TRIGGER", TRIGGER_GROUP_NAME);
			sched.pauseTrigger(triggerKey);
			sched.unscheduleJob(triggerKey);
			JobKey jobKey = new JobKey(jobName + "_JOB", JOB_GROUP_NAME);
			sched.deleteJob(jobKey);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @description 启动所有定时任务
	 * @author 王璞
	 * @date 2017年10月24日下午6:30:37
	 */
	public static void startJobs() {
		Scheduler sched = null;
		try {
			sched = gSchedulerFactory.getScheduler();
			sched.start();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @description 关闭所有定时任务
	 * @author 王璞
	 * @date 2017年10月24日下午6:30:24
	 */
	public static void shutdownJobs() {
		Scheduler sched = null;
		try {
			sched = gSchedulerFactory.getScheduler();
			if (!sched.isShutdown()) {
				sched.shutdown();
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}
